import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/mine/apartment_manage/water_electricity_setting/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";

class PriceSetting extends StatefulWidget {
  const PriceSetting({super.key});

  @override
  State<PriceSetting> createState() => _PriceSettingState();
}

class _PriceSettingState extends State<PriceSetting> {

  Map _priceConfig = {};

  // 获取电费单价配置
  void _getPriceConfig() async {
    Map resData = await waterElectricitySettingApi.getConfigInfo();
    setState(() {
      _priceConfig = resData["data"] ?? {};
    });
  }

  // 编辑电费单价
  void _editPrice() async {
    Stacker.pushNamed(Keys.WATER_ELECTRICITY_ELECTRICITY_PRICE_SETTING).then((result) {
      if (result != null && result == true) {
        _getPriceConfig();
      }
    });

  }

  @override
  void initState() {
    _getPriceConfig();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "电费单价设置",
      child: Container(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        child: MenuBlock(
          items: [
            MenuBlockItem(
              text: "电费单价",
              suffix: Text("${_priceConfig["kwhPrice"]}元/度"),
              onTap: () {
                _editPrice();
              },
            ),
          ],
        ),
      ),
    );
  }
}